//TODO sameer

#include <fstream>
#include "pthread.h"
#include "Dispatcher.h"
#include "Request.h"
#include "Logger.h"
#include <iostream>

Dispatcher::Dispatcher( std::string ReqFile, Logger& c_logme, Logger& Err ):LOGGER( c_logme ),ERRLOGGER (Err )
{

	m_strReqFile = ReqFile;
};


ReqIterator Dispatcher::GetBeginIterator()
{
	return m_pRequests.begin();
};

ReqIterator Dispatcher::GetEndIterator()
{
	return m_pRequests.end();
};

bool Dispatcher::ParseRequests(){

	std::ifstream ReqFile;

	//LOGGER<<"Entering Dispatcher::ParseRequests \n";
	//std::cout<<"Entering Dispatcher::ParseRequests \n";
	LOGGER<<"Dispatcher::ParseRequests "<<"m_strReqFile: "<<m_strReqFile<<"\n";

	ReqFile.open( m_strReqFile.c_str() );

	if( !ReqFile )
	{
		LOGGER<<"Dispatcher::ParseRequests "<<" File is not open, Check file name."<<"\n";
		return false;
	}

	std::string strIPAddress 	= "NONE";
	std::string strTimeStamp 	= "NONE";
	std::string strType		= "NONE";
	std::string strURL		= "NONE";
	std::string Line ;

	while(  ReqFile.good() )
	{
		getline( ReqFile, Line );

		if( Line.length() != 0 ){
		LOGGER<<"Dispatcher::ParseRequests "<<"Line is:"<<Line<<"\n";

		}
		else{
			LOGGER<<"Dispatcher::ParseRequests "<<"Line empty, continue"<<"\n";

			continue;
		}

		//Ignore Comments.
		if( Line.find('#') !=  std::string::npos )
		{
			Line.erase( Line.find_first_of('#') );

			if( Line.length() != 0 ){
			LOGGER<<"trimmed Line is:" << Line<<"\n";
			}
			else{
				LOGGER<<"Dispatcher::ParseRequests "<<"After trimming line became empty,continue"<<"\n";
				continue;
			}
		}
		else
		{
			LOGGER<<"Dispatcher::ParseRequests "<<"trimmed Line is:"<< Line<<"\n";
		}

		strIPAddress	=	Line.substr( 0, Line.find_first_of( '\t' ));
		std::string tempString = Line.substr( Line.find_first_of( '\t' ) + 1, Line.length());
		strTimeStamp	=	tempString.substr( 0, tempString.find_first_of( '\t' ));
		tempString = tempString.substr(tempString.find_first_of( '\t' ) + 1, tempString.length());
		strType			=	tempString.substr( 0, tempString.find_first_of( '\t' ));
		tempString = tempString.substr(tempString.find_first_of( '\t' ) + 1, tempString.length());
		strURL			=	tempString.substr( 0 , tempString.length());
		std::string	strFileName  = strURL;
		std::size_t	Idx	= strURL.find_last_of( "/" );

		if( Idx != std::string::npos )
		{
			strFileName = strURL.substr( Idx + 1, strURL.length()  - Idx );
		}



		Request* pReq		= new Request();
		pReq->m_IPAddress 	= strIPAddress;
		pReq->m_TimeStamp	= strTimeStamp;
		pReq->m_strURL		= strURL;
		pReq->m_strFileName	= strFileName;
		pReq->m_ReqType		= ( strType == "PUT" ?  PUT : ( strType == "GET" ) ? GET : UNKNOWN ) ;

		m_pRequests.push_back( pReq  );
		LOGGER<<"Dispatcher::ParseRequests "<<"pReq->m_strURL "<<pReq->m_strURL<<"\n";
		LOGGER<<"Dispatcher::ParseRequests "<<"pReq->m_strType:"<<strType<<"\n";
		LOGGER<<"\n";
	}

	ReqFile.close();
	return true;
};

void Dispatcher::PrintRequests()
{

	std::vector<Request*>::iterator itpReq  ;
	LOGGER<<"Dispatcher::ParseRequests "<<"Current requests\n";

	for( itpReq = m_pRequests.begin() ; itpReq < m_pRequests.end(); itpReq++ )
	{
		(*itpReq)->Print( LOGGER );
	}
};

void Dispatcher::Eraserequests()
{

	m_pRequests.clear();
}

